/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-27     xuan       the first version
 * 呼吸灯
 */
#include<rtthread.h>
#include<math.h>
#include"board.h"

#define PWM_DEV_NAME "t5pwm" //PWM设备名称
#define PWM_DEV_CHANNEL 1   //PWM通道
struct rt_device_pwm *pwm_dev;  //PWM设备句柄
ALIGN(RT_ALIGN_SIZE)
uint8_t PWM_Thread_Stack[1024];
void PWM_Thread_Entry(void *para);
rt_thread_t pwm_thread;
rt_uint32_t period,pulse;

void Pwm_Init(void){
    period=1000000; //周期=1M/period kHz
    pulse=0;
    pwm_dev=(struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    RT_ASSERT(pwm_dev!=RT_NULL);
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);//设置PWM周期和脉冲宽度
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);//使能设备
}

void PWM_Thread_Entry(void *para){
    Pwm_Init();
    while(1){
        //使红灯闪烁
        for (int var = 0; var < 200; ++var) {
            pulse=period*sin(M_PI/199*var);
            rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
            rt_thread_mdelay(15);
        }
        rt_thread_mdelay(100);
    }
}

int Pwm_Thread_Init(void){
    pwm_thread=rt_thread_create("pwm_thread", PWM_Thread_Entry, RT_NULL, sizeof(PWM_Thread_Stack), 10, 10);
    rt_thread_startup(pwm_thread);
    return 0;
}

//INIT_APP_EXPORT(Pwm_Thread_Init);
